/*
 * 文 件 名:  DispatchAction.java
 * 版    权:  Tydic Technologies Co., Ltd. Copyright 1993-2013,  All rights reserved
 * 描    述:  <描述>
 * 修 改 人:  yanglong
 * 修改时间:  2013-11-10
 * 跟踪单号:  <需求跟踪单号>
 * 修改单号:  <需求修改单号>
 * 修改内容:  <修改内容>
 */
package com.hfut.jpoa.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hfut.jpoa.common.action.AbsAction;
import com.hfut.jpoa.common.dto.Pagination;
import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.common.util.ConstantUtil;
import com.hfut.jpoa.dto.DispatchDTO;
import com.hfut.jpoa.model.Customer;
import com.hfut.jpoa.model.Dispatch;
import com.hfut.jpoa.model.Syspara;
import com.hfut.jpoa.model.User;
import com.hfut.jpoa.service.CustomerService;
import com.hfut.jpoa.service.DispatchService;
import com.hfut.jpoa.service.LogService;
import com.hfut.jpoa.service.SysparaService;
import com.hfut.jpoa.service.UserService;
import com.hfut.jpoa.service.exception.CustomerException;
import com.hfut.jpoa.service.exception.DispatchException;
import com.hfut.jpoa.service.exception.LogException;
import com.hfut.jpoa.service.exception.SysparaException;
import com.hfut.jpoa.service.exception.UserException;

/**
 * <一句话功能简述> <功能详细描述>
 * 
 * @author yanglong
 * @version [1.0, 2013-11-10]
 * @since [产品/1.0]
 */
public class DispatchAction extends AbsAction<DispatchDTO>
{

	private static final Logger LOGGER = LoggerFactory
	        .getLogger(DispatchAction.class);

	private static final long serialVersionUID = -7819502638545092791L;

	private DispatchService dispatchService;

	private Dispatch dispatch;

	private List<Dispatch> lstDispatchs;

	private LogService logService;

	/**
	 * 标志位：0-增加 1-修改 2-详情
	 */
	private String actionType;

	/**
	 * 页面类型：0：调度中心 1：售后部
	 */
	private String pageType;

	/**
	 * 包含分页接受前台变量
	 */
	private DispatchDTO dispatchDTO;

	/**
	 * 分页结果集
	 */
	private ResultDto<Dispatch> resultDto;

	/**
	 * 总页数
	 */
	private int totalPage;

	/**
	 * 用户列表
	 */
	private List<User> lstUsers;

	/**
	 * 创建者,以后从session中取
	 */
	private String creator = "yl";

	private String operaterIp = "127.0.0.1";

	/**
	 * 用户服务
	 */
	private UserService userService;

	/**
	 * 客户服务
	 */
	private CustomerService customerService;

	/**
	 * 字典服务
	 */
	private SysparaService sysparaService;

	/**
	 * 路线列表
	 */
	private List<Syspara> lstPath;

	/**
	 * 报修类型
	 */
	private List<Syspara> lstDispatchTypes;

	/**
	 * 加载选择项目弹出层
	 * 
	 * @return
	 */
	public String initDialog() throws DispatchException
	{
		initDate();
		return "init_dialog_list";
	}

	/**
	 * 请求业务中心
	 * 
	 * @return
	 * @throws DispatchException
	 */
	public String toBusinessCenter() throws DispatchException
	{
		try
		{
			logService.insertLog(creator, operaterIp, "调度", "查询", "请求业务中心",
			        "请求业务中心");
		} catch (LogException e)
		{
			LOGGER.error("新增日志异常：", e);
			throw new DispatchException("新增日志异常：", e);
		}
		return "to_business_center";
	}

	/**
	 * 请求管理中心
	 * 
	 * @return
	 * @throws DispatchException
	 */
	public String toManageCenter() throws DispatchException
	{
		return "to_manage_center";
	}

	/**
	 * 管理中心审核列表初始化
	 * 
	 * @return
	 * @throws DispatchException
	 */
	public String toReviewList() throws DispatchException
	{
		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage") == null ? ""
		        : request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (dispatchDTO == null)
		{
			dispatchDTO = new DispatchDTO();
		}
		lstUsers = initLstUser(new User());
		dispatchDTO.setPagination(pagination);
		resultDto = dispatchService.queryInfoForPage(dispatchDTO);
		actionType = ConstantUtil.ACTIONTYPE_REVIEW;

		// 封装用户信息，讲用户ID转成名称
		dispatchService.packageUser(resultDto.getResult(), lstUsers);
		totalPage = resultDto.getPagination().getTotalPage();

		return "list";
	}

	public String listDialog() throws DispatchException
	{
		// 加载线路
		lstPath = findLstPath(ConstantUtil.PATH_ID);

		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage") == null ? ""
		        : request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (dispatchDTO == null)
		{
			dispatchDTO = new DispatchDTO();
		}
		lstUsers = initLstUser(new User());
		dispatchDTO.setPagination(pagination);
		resultDto = dispatchService.queryInfoForPage(dispatchDTO);

		// 封装用户信息，讲用户ID转成名称
		dispatchService.packageUser(resultDto.getResult(), lstUsers);
		totalPage = resultDto.getPagination().getTotalPage();

		return "dispatch_dialog_list";
	}

	/**
	 * 记载路线列表
	 * 
	 * @return
	 * @throws DispatchException
	 */
	private List<Syspara> findLstPath(String strType) throws DispatchException
	{
		// 从字典表中加载列表
		List<Syspara> lstPathTemp = new ArrayList<Syspara>();
		try
		{
			Syspara syspara = new Syspara();
			syspara.setType(strType);
			lstPathTemp = sysparaService.querySysparaList(syspara);
		} catch (SysparaException e)
		{
			LOGGER.error("初始化" + strType + "异常异常：", e);
			throw new DispatchException("初始化" + strType + "异常异常：", e);
		}
		return lstPathTemp;
	}

	/**
	 * 加载客户列表
	 */
	private void listCustomers() throws DispatchException
	{
		Customer customer = new Customer();
		lstPath = findLstPath(ConstantUtil.PATH_ID);
		try
		{
			customerService.queryCustomers(customer);
		} catch (CustomerException e)
		{
			LOGGER.error("加载客户信息异常：", e);
			throw new DispatchException("加载客户信息异常：", e);
		}

	}

	/**
	 * 加载售后报价列表
	 * 
	 * @return
	 */
	public String customerQuoteInit() throws DispatchException
	{

		return "dispatch_list_init";
	}

	/**
	 * 加载列表页显示数据
	 */
	private void initDate() throws DispatchException
	{
		lstPath = findLstPath(ConstantUtil.PATH_ID);
		DispatchDTO dto = new DispatchDTO();
		Pagination pagination = new Pagination(0);
		dto.setPagination(pagination);
		lstUsers = initLstUser(new User());
		pageType = ConstantUtil.PAGETYPE_DISPATCH;
		resultDto = dispatchService.queryInfoForPage(dto);
		totalPage = resultDto.getPagination().getTotalPage();
	}

	/**
	 * 加载调度中心分页列表
	 * 
	 * @return
	 * @throws CategoryException
	 */
	public String listInit() throws DispatchException
	{
		initDate();
		actionType = ConstantUtil.ACTIONTYPE_LIST;
		return "dispatch_list_init";
	}

	/**
	 * 加载售后部分页列表
	 * 
	 * @return
	 * @throws CategoryException
	 */
	public String customerListInit() throws DispatchException
	{
		lstPath = findLstPath(ConstantUtil.PATH_ID);
		DispatchDTO dto = new DispatchDTO();
		Pagination pagination = new Pagination(0);
		dto.setPagination(pagination);
		lstUsers = initLstUser(new User());
		pageType = ConstantUtil.PAGETYPE_CUSTOMER;
		resultDto = dispatchService.queryInfoForPage(dto);
		totalPage = resultDto.getPagination().getTotalPage();
		actionType = ConstantUtil.ACTIONTYPE_LIST;
		return "dispatch_list_init";
	}

	/**
	 * 加载列表数据
	 * 
	 * @return
	 */
	public String list() throws DispatchException
	{

		// 加载线路
		lstPath = findLstPath(ConstantUtil.PATH_ID);

		HttpServletRequest request = ServletActionContext.getRequest();
		String currentPage = request.getParameter("currentPage");
		Pagination pagination = new Pagination(Integer.parseInt(currentPage));
		String pagesize = request.getParameter("pageSize");
		pagination.setPageSize(Integer.parseInt(pagesize));
		if (dispatchDTO == null)
		{
			dispatchDTO = new DispatchDTO();
		}
		lstUsers = initLstUser(new User());
		dispatchDTO.setPagination(pagination);
		resultDto = dispatchService.queryInfoForPage(dispatchDTO);

		// 封装用户信息，讲用户ID转成名称
		dispatchService.packageUser(resultDto.getResult(), lstUsers);
		totalPage = resultDto.getPagination().getTotalPage();

		return "list";
	}

	/**
	 * 增加页面初始化
	 */
	public String addInit() throws DispatchException
	{
		// 加载路线
		lstPath = findLstPath(ConstantUtil.PATH_ID);

		// 加载报修类型
		lstDispatchTypes = findLstPath(ConstantUtil.DISPATCH_TYPE);

		actionType = ConstantUtil.ACTIONTYPE_ADD;

		// 加载员工信息
		lstUsers = initLstUser(new User());
		return "dispatch_add_init";
	}

	/**
	 * 加载用户列表
	 * 
	 * @param user
	 * @return
	 * @throws DispatchException
	 */
	public List<User> initLstUser(User user) throws DispatchException
	{
		// 加载员工
		if (null == user)
		{
			user = new User();
		}
		List<User> lstUser;
		try
		{
			lstUser = userService.queryUser(user);
		} catch (UserException e)
		{
			LOGGER.error("加载员工异常：", e);
			throw new DispatchException("加载员工异常：", e);
		}
		return lstUser;
	}

	/**
	 * 新增保存
	 * 
	 * @return
	 */
	public String add() throws DispatchException
	{
		dispatch.setFlag(ConstantUtil.FLAG_DISPATCH);
		try
		{
			dispatchService.saveDispatch(dispatch);
		} catch (DispatchException e)
		{
			LOGGER.error("加载员工异常：", e);
			throw new DispatchException("加载员工异常：", e);
		}
		return "dispatch_list_init";
	}

	/**
	 * 修改页面初始化
	 */
	public String dispatchUpdateInit() throws DispatchException
	{
		// 加载路线
		lstPath = findLstPath(ConstantUtil.PATH_ID);
		// 加载员工信息
		lstUsers = initLstUser(new User());

		actionType = ConstantUtil.ACTIONTYPE_UPDATE;
		Dispatch resultDispatch = dispatchService.findInfo(dispatch);
		dispatch = resultDispatch;

		return "dispatch_add_init";
	}

	/**
	 * 回访页面初始化
	 * 
	 * @return
	 * @throws DispatchException
	 */
	public String dispatchVisitInit() throws DispatchException
	{
		// 加载路线
		lstPath = findLstPath(ConstantUtil.PATH_ID);
		// 加载员工信息
		lstUsers = initLstUser(new User());
		actionType = ConstantUtil.ACTIONTYPE_VISIT;
		Dispatch resultDispatch = dispatchService.findInfo(dispatch);
		dispatch = resultDispatch;

		return "dispatch_add_init";
	}

	/**
	 * 回访
	 * 
	 * @return
	 * @throws DispatchException
	 */
	public String dispatchVisit() throws DispatchException
	{
		try
		{
			dispatchService.update(dispatch);
		} catch (DispatchException e)
		{
			LOGGER.error("dispatchVisit error：", e);
			throw new DispatchException("dispatchVisit error：", e);
		}
		return "dispatch_list_init";
	}

	/**
	 * 修改字典表
	 * 
	 * @return
	 * @throws DispatchException
	 */
	public String updateDispatch() throws DispatchException
	{

		// dispatchService.updateDispatch(dispatch);
		return "dispatch_list_init";
	}

	/**
	 * 修改字典表状态
	 */
	public void updateState()
	{
		/*
		 * try { // dispatchService.updateDispatch(dispatch); } catch
		 * (DispatchException e1) { try { failure("操作异常：" + e1.getMessage()); }
		 * catch (IOException e) { LOGGER.error("修改字典配置异常", e); } } try {
		 * success("成功操作！"); } catch (IOException e) { LOGGER.error("修改字典配置异常",
		 * e); }
		 */
	}

	/**
	 * 验证编码是否存在
	 * 
	 * @throws IOException
	 */
	public void codeValidator() throws IOException
	{
		/*
		 * boolean flag = false; try { flag =
		 * dispatchService.codeValidator(dispatch); } catch (DispatchException
		 * e) { LOGGER.error("验证编码是否存在异常", e); failure("验证编码是否存在异常" +
		 * e.getMessage()); } if (flag) { failure("该编码已存在，请重新输入"); } else {
		 * success("输入正确，该编码可以使用"); }
		 */
	}

	public String getActionType()
	{
		return actionType;
	}

	public void setActionType(String actionType)
	{
		this.actionType = actionType;
	}

	public DispatchService getDispatchService()
	{
		return dispatchService;
	}

	public void setDispatchService(DispatchService dispatchService)
	{
		this.dispatchService = dispatchService;
	}

	public Dispatch getDispatch()
	{
		return dispatch;
	}

	public void setDispatch(Dispatch dispatch)
	{
		this.dispatch = dispatch;
	}

	public List<Dispatch> getLstDispatchs()
	{
		return lstDispatchs;
	}

	public void setLstDispatchs(List<Dispatch> lstDispatchs)
	{
		this.lstDispatchs = lstDispatchs;
	}

	public DispatchDTO getDispatchDTO()
	{
		return dispatchDTO;
	}

	public void setDispatchDTO(DispatchDTO dispatchDTO)
	{
		this.dispatchDTO = dispatchDTO;
	}

	public ResultDto<Dispatch> getResultDto()
	{
		return resultDto;
	}

	public void setResultDto(ResultDto<Dispatch> resultDto)
	{
		this.resultDto = resultDto;
	}

	public int getTotalPage()
	{
		return totalPage;
	}

	public void setTotalPage(int totalPage)
	{
		this.totalPage = totalPage;
	}

	public UserService getUserService()
	{
		return userService;
	}

	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

	public List<User> getLstUsers()
	{
		return lstUsers;
	}

	public void setLstUsers(List<User> lstUsers)
	{
		this.lstUsers = lstUsers;
	}

	public String getPageType()
	{
		return pageType;
	}

	public void setPageType(String pageType)
	{
		this.pageType = pageType;
	}

	public CustomerService getCustomerService()
	{
		return customerService;
	}

	public void setCustomerService(CustomerService customerService)
	{
		this.customerService = customerService;
	}

	public List<Syspara> getLstPath()
	{
		return lstPath;
	}

	public void setLstPath(List<Syspara> lstPath)
	{
		this.lstPath = lstPath;
	}

	public SysparaService getSysparaService()
	{
		return sysparaService;
	}

	public void setSysparaService(SysparaService sysparaService)
	{
		this.sysparaService = sysparaService;
	}

	public List<Syspara> getLstDispatchTypes()
	{
		return lstDispatchTypes;
	}

	public void setLstDispatchTypes(List<Syspara> lstDispatchTypes)
	{
		this.lstDispatchTypes = lstDispatchTypes;
	}

	public LogService getLogService()
	{
		return logService;
	}

	public void setLogService(LogService logService)
	{
		this.logService = logService;
	}

}
